package com.example.zyz.demo_sql_grid_list_notification_dialog;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/*
该demo应用gridview形成操作界面，点击进入添加和显示界面，显示界面又listview从数据库读取数据，
录入由dialog界面录入基本信息，成功后会显示notification

 */
public class MainActivity extends AppCompatActivity implements OnItemClickListener {

    //生成gridview界面
    private SimpleAdapter sa;
    private GridView gv;
    private int []iconarray={R.drawable.aaa,R.drawable.ddd,R.drawable.ccc,R.drawable.xxx};
    private String []griditemstr={"显示","添加","删除","更新"};
    private List<Map<String,Object>> datalist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        datalist=new ArrayList<Map<String, Object>>();
        gv= (GridView) findViewById(R.id.gridView);



        sa=new SimpleAdapter(this,getdata(),R.layout.layout_griditem,new String[] {"1","2"},new int[]{R.id.imageButtonitem,R.id.text_item});
        gv.setAdapter(sa);
        gv.setOnItemClickListener(this);


//        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });

    }
    //map的key，只跟sa上面的第四个参数有关，不要把他和上面的string 数组联系起来，没有关系。key的数量和图像，文本的类型数量有关，类型数量！

    private List<Map<String, Object>> getdata() {
        for (int i=0;i<griditemstr.length;i++)
        {
            Map<String,Object>ma=new HashMap<String,Object>();
            ma.put("1",iconarray[i]);
            ma.put("2",griditemstr[i]);
            datalist.add(ma);
        }
        return datalist;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i("info","这里点击的是第"+position);
        switch (position)
        {
            case 0:
                Intent intent1=new Intent();
                intent1.setClass(this,listviewactivity.class);
                startActivity(intent1);
//                this.finish();
                break;

            case 1:
//                AlertDialog.Builder builder=new AlertDialog.Builder(this);
//                builder.setPositiveButton("据说这里是dialog的名字", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        Toast.makeText(MainActivity.this,"点击dialog产生这个东西",Toast.LENGTH_SHORT).show();
//                        Log.i("info","这里点击了button");
//                    }
//                });
//                builder.show();
                Intent intent=new Intent();
                intent.setClass(MainActivity.this, add_dbactivity.class);
                startActivity(intent);
//                MainActivity.this.finish();
                break;
            case 2:

                break;

        }
        Toast.makeText(MainActivity.this,"这里才是点击产生的toast",Toast.LENGTH_SHORT).show();

//        Intent intent=new Intent();
//        intent.setClass(this,listviewactivity.class);
//        startActivity(intent);
//        MainActivity.this.finish();
    }
}
